import { defineStore } from "pinia";
import auth from '@/api/auth.ts'
import { doLogout } from '@/common/plugins/Axios'
import { ElNotification } from "element-plus";
import constants from '@/common/constants'
import config from '@/config'
import { store } from '../index'
import { toSSO } from '@/common/util'
import WaterMark from '@/common/watermark'

export const userStore = defineStore('user', {
  state: () => {
    return {
      userInfo: null, // 登录信息存储字段-建议每个项目换一个字段，避免与其它项目冲突
      userName: null,
      accessToken: null,
      jwtToken: null,
      menus: null,
      robotFlag: false,
      robotUrl: null,
    }
  },
  getters: {
    getUserInfo(): string {
      return this.userInfo
    },
    getUserName(): string {
      return this.userName
    },
    getAccessToken(): string {
      return this.accessToken
    },
    getJwtToken(): string {
      return this.jwtToken
    },
    getMenus(): string {
      return this.menus
    },
    getRobotFlag(): boolean {
      return this.robotFlag
    },
    getRobotUrl(): string {
      return this.robotUrl
    },
  },
  actions: {
    setUserInfo(userInfo: string) {
      this.userInfo = userInfo
    },
    setUserName(userName: string) {
      this.userName = userName
    },
    setAccessToken(accessToken: string) {
      this.accessToken = accessToken
    },
    setMenus(menus: string) {
      this.menus = menus
    },
    setNoJwtToken(jwtToken: string) {
      this.jwtToken = jwtToken
    },
    async setJwtToken(code) {
      const tokenRes = await auth.getJwtToken(code)
      console.log("tokenRes===>",tokenRes.data.code);
      if (tokenRes === undefined) {
        doLogout()
      }else if(tokenRes.data.code==="500"){
        toSSO()
      }
      let { code: tokenState, data: tokenData } = tokenRes.data
      if (constants.RESP_CODE_SUCESS !== tokenState) {
        ElNotification.error({
          title: '提示',
          message: '接口异常'
        })
      }
      let { jwtToken, accessToken } = tokenData
      this.jwtToken = jwtToken
      this.accessToken = accessToken
      const principalRes = await auth.getUser()
      let { code: principalState, data: principal } = principalRes.data
      this.userInfo = principal
      let { username, menus, loginAcct } = principal
      let { latnId, orgId, orgName, staffName, dataArea } = principal.user
      this.userName = username
      console.log("principal=====>",principal)
      // WaterMark.set(loginAcct+" "+staffName+" "+orgName);
      localStorage.setItem("uname", loginAcct)
      localStorage.setItem("lantId", latnId)
      localStorage.setItem("orgId", orgId)
      localStorage.setItem("orgName", orgName)
      localStorage.setItem("staffName", staffName)
      localStorage.setItem("menus", JSON.stringify(menus))
      localStorage.setItem("token",this.jwtToken)
      localStorage.setItem("accessToken",this.accessToken)
      localStorage.setItem("powerTableList", JSON.stringify(dataArea))
    },
  },
  persist: [{
    key: constants.PRINCIPAL_KEY,
    storage: sessionStorage,
    paths: ['userInfo']
  }, {
    key: constants.TOKEN_KEY,
    storage: sessionStorage,
    paths: ['jwtToken']
  }, {
    key: constants.USER_KEY,
    storage: sessionStorage,
    paths: ['userName']
  }, {
    key: constants.MENU_KEY,
    storage: sessionStorage,
    paths: ['menus']
  }, {
    key: 'robotFlag',
    storage: sessionStorage,
    paths: ['robotFlag']
  }, {
    key: 'robotUrl',
    storage: sessionStorage,
    paths: ['robotUrl']
  }],
})

export const userStoreWithOut = () => {
  return userStore(store)
}